/*
 * PlayersDialog.java
 *
 * Created on September 8, 2008, 1:30 PM
 */

package dnl.games.stragego.ui;

import javax.swing.table.AbstractTableModel;

/**
 * 
 * @author daniel
 */
public class PlayersDialog extends javax.swing.JDialog {

	private String rival;

	/** Creates new form PlayersDialog */
	public PlayersDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public void setPlayerNames(String[] playerNames) {
		if(playerNames == null){
			System.out.println();
		}
		TModel model = new TModel(playerNames);
		playersTable.setModel(model);
	}

	class TModel extends AbstractTableModel {
		String[] playersNames;

		TModel(String[] playersNames) {
			this.playersNames = playersNames;
		}

		@Override
		public Object getValueAt(int row, int column) {
			if (column == 0)
				return playersNames[row];
			return "";
		}

		@Override
		public String getColumnName(int column) {
			if (column == 0)
				return "name";
			return "g";
		}

		@Override
		public int getColumnCount() {
			return 2;
		}

		@Override
		public int getRowCount() {
			return playersNames.length;
		}

	}

	public String getRival() {
		return rival;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		playersTable = new javax.swing.JTable();
		challengJB = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		playersTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

		}, new String[] { "Name", " ", "a" }) {
			Class[] types = new Class[] { java.lang.String.class, java.lang.Object.class,
					java.lang.Boolean.class };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}
		});
		jScrollPane1.setViewportView(playersTable);

		challengJB.setText("Challenge");
		challengJB.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				challengJBActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,
						javax.swing.GroupLayout.PREFERRED_SIZE, 235,
						javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addComponent(
						challengJB).addContainerGap(67, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap(13, Short.MAX_VALUE).addComponent(
						jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 267,
						javax.swing.GroupLayout.PREFERRED_SIZE).addGap(20, 20, 20)).addGroup(
				layout.createSequentialGroup().addGap(74, 74, 74).addComponent(challengJB)
						.addContainerGap(200, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void challengJBActionPerformed(java.awt.event.ActionEvent evt) {// GEN
																			// -
																			// FIRST
																			// :
																			// event_challengJBActionPerformed
		int i = playersTable.getSelectedRow();
		rival = (String) playersTable.getModel().getValueAt(i, 0);
		this.setVisible(false);
	}// GEN-LAST:event_challengJBActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				PlayersDialog dialog = new PlayersDialog(new javax.swing.JFrame(), true);
				 dialog.setPlayerNames(new String[]{"A", "B"});
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton challengJB;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable playersTable;
	// End of variables declaration//GEN-END:variables

}
